﻿var APP = (function (app) {
    data = {
        AuditId: '',
        UserName: '',
        WorkflowId: '',
        WorktaskId: '',
        WorkflowInsId: '',
        WorktaskInsId: '',
        OperatorInsId: '',
        Arch: '', //部门
        Duty: '', //职务
        Status: '',
        TransmitString: '', // 转发
        ApprovalMessage: ''// 审核意见
    };
    var _ApprovalEntity = null; //审批实体


    //审批实体
    function ApprovalEntity() {
        this.AuditId = '';   //审批记录编号
        this.UserName = '';  //审批人
        this.Arch = '';      //部门
        this.AuditTime = ''; //审批时间
        this.Duty = '';      //职务
        this.Status = '';    //审批记录状态
    };


    app.init = function (username, workFlowId, workTaskId, workFlowInsId, worktaskInsId, operatorInsId) {
        data.UserName = username;
        data.WorkflowId = workFlowId;
        data.WorktaskId = workTaskId;
        data.WorkflowInsId = workFlowInsId;
        data.WorktaskInsId = worktaskInsId;
        data.OperatorInsId = operatorInsId;

        APP.ApprovalInit.init(username, operatorInsId); //初始审批
    };

    app.NewApprovalEntity = function () {
        var appEntity = new ApprovalEntity();
        appEntity.AuditId = '';   //审批记录编号
        appEntity.UserName = '';  //审批人
        appEntity.Arch = '';      //部门
        appEntity.AuditTime = ''; //审批时间
        appEntity.Duty = '';      //职务
        appEntity.Status = '';    //审批记录状态
        return appEntity;
    };

    app.setWorkflowInsId = function (workFlowInsId) {
        data.WorkflowInsId = workFlowInsId;
    };
    app.setWorktaskInsId = function (worktaskInsId) {
        data.WorktaskInsId = worktaskInsId;
    };
    app.setUserName = function (userName) {
        data.UserName = userName;
    };
    app.setOperatorInsId = function (operatorInsId) {
        data.OperatorInsId = operatorInsId;
    };
    /// 转发
    app.setTransmitString = function (transmitString) {
        data.TransmitString = transmitString;
    };
    /// 审核意见
    app.setApprovalMessage = function (approvalMessage) {
        data.ApprovalMessage = approvalMessage;
    };

    app.setApprovalEntity = function (appEntity) {
        _ApprovalEntity = appEntity
    };
    app.getApprovalEntity = function () {
        return _ApprovalEntity;
    };
    app.getOperatorInsId = function () {
        var strOperatorInsId = '';
        if (data) {
            strOperatorInsId = data.OperatorInsId;
        }
        return strOperatorInsId;
    };

    //流程ID
    app.getWorkflowId = function () {
        var strWorkflowId = '';
        if (data) {
            strWorkflowId = data.WorkflowId;
        }
        return strWorkflowId;
    };

    //流程任务ID
    app.getWorktaskId = function () {
        var strWorktaskId = '';
        if (data) {
            strWorktaskId = data.WorktaskId;
        }
        return strWorktaskId;
    };
    //流程实例ID
    app.getWorkflowInsId = function () {
        var strWorkflowInsId = '';
        if (data) {
            strWorkflowInsId = data.WorkflowInsId;
        }
        return strWorkflowInsId;
    };
    //流程任务实例ID
    app.getWorktaskInsId = function () {
        var strWorktaskInsId = '';
        if (data) {
            strWorktaskInsId = data.WorktaskInsId;
        }
        return strWorktaskInsId;
    };
    //部门
    app.getArch = function () {
        var strArch = '';
        if (data) {
            strArch = data.Arch;
        }
        return strArch;
    };
    //职务
    app.getDuty = function () {
        var strDuty = '';
        if (data) {
            strDuty = data.Duty;
        }
        return strDuty;
    };
    //状态
    app.getStatus = function () {
        var strStatus = '';
        if (data) {
            strStatus = data.Status;
        }
        return strStatus;
    };
    //审批人
    app.getUserName = function () {
        var strUserName = '';
        if (data) {
            strUserName = data.UserName;
        }
        return strUserName;
    };
    return app;
})(APP || {});

//审批初始化
APP.ApprovalInit = (function (appinit) {
    var _baseAPIUrl = "WorkflowDataMobileManager.aspx/ApprovalInit"; //
    var _currentParams = [];
    var _loaded = false;

    var _UserName = "";
    var _OperatorInsId = "";

    appinit.init = function (UserName, OperatorInsId) {
        _UserName = UserName;
        _OperatorInsId = OperatorInsId;
        _loaded = false;
        APP.ApprovalInit.open();
    };

    appinit.open = function () {
        if (!_loaded) {
            _load();
            _loaded = true;
        }
    };

    function _load(append) {
        _currentParams = {
            UserName: _UserName,
            OperatorInsId: _OperatorInsId
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            var person = JSON.parse(data);
            if (person) {
                var app = person;
                var appEntity = APP.NewApprovalEntity();
                appEntity.AuditId = app.AuditId;   //审批记录编号
                appEntity.UserName = app.UserName;  //审批人
                appEntity.Arch = app.Arch;      //部门
                appEntity.AuditTime = app.AuditTime; //审批时间
                appEntity.Duty = app.Duty;      //职务
                appEntity.Status = app.Status;    //审批记录状态

                APP.setApprovalEntity(appEntity);
            }
        }
    };
    return appinit;
})(WF.ApprovalInit || {});


//审批意见保存
APP.SaveApproval = (function (saveapp) {
    var _baseAPIUrl = "WorkflowDataMobileManager.aspx/SaveApproval"; //审批意见保存
    var _currentParams = [];
    var _loaded = false;

    var _UserName = '';
    var _TransmitString = '';
    var _AuditId = '';
    var _WorkFlowId = '';
    var _WorkTaskId = '';
    var _WorkFlowInsId = '';
    var _WorktaskInsId = '';
    var _OperatorInsId = '';
    var _ApprovalMessage = '';
    var _ApprovalStatus = '';
    var _IsSuccess = '0';
    var _UserNames='';//转发人姓名字符串
    //transmitString： 转发
    //approvalMessage: 审核意见
    saveapp.init = function (transmitString,userNames,approvalMessage) {
        _UserName = APP.getUserName();
        _TransmitString = transmitString;

        _WorkFlowId = APP.getWorkflowId();
        _WorkTaskId = APP.getWorktaskId();
        _WorkFlowInsId = APP.getWorkflowInsId();
        _WorktaskInsId = APP.getWorktaskInsId();
        _OperatorInsId = APP.getOperatorInsId();
        _ApprovalMessage = approvalMessage;
        _UserNames=userNames;

      
        var appEntity = APP.getApprovalEntity();
        if (appEntity) {
            _AuditId = appEntity.AuditId;
            _ApprovalStatus = appEntity.Status;
        }

        APP.SaveApproval.open();
    };

    saveapp.open = function () {
        _load();
    };

    function _load(append) {
        _currentParams = {
            UserName: _UserName,
            TransmitString: _TransmitString,
            UserNames:_UserNames,
            AuditId: _AuditId,
            WorkFlowId: _WorkFlowId,
            WorkTaskId: _WorkTaskId,
            WorkFlowInsId: _WorkFlowInsId,
            WorktaskInsId: _WorktaskInsId,
            OperatorInsId: _OperatorInsId,
            ApprovalMessage: _ApprovalMessage,
            ApprovalStatus: _ApprovalStatus
        };
        var callback = append === true ?
        function (data) {
            _populate(data, true)
        } : _populate;
        Query.execute(_baseAPIUrl, _currentParams, callback)
    }
    function _populate(data, append) {
        if (data) {
            if (data.indexOf('OK%') >= 0) {
                _IsSuccess = '1';
                // alert("审批意见提交成功！");
            }
            else {
                _IsSuccess = '0';
                var dataArray = resp.split("@");
                alert("审批意见提交失败" + dataArray[1]);
            }
        }
    };
    saveapp.getIsSuccess = function () {
        return _IsSuccess;
    };
    return saveapp;
})(WF.SaveApproval || {});

